package com.cskaoyan.service;

import com.cskaoyan.bean.*;
import com.cskaoyan.mapper.CategoryMapper;
import com.cskaoyan.mapper.StorageMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.lang.System;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

/**
 * @author Tangyin
 * @description
 * @since 2022/05/07 13:31
 */
@Service
public class AdminCategoryServiceImpl implements AdminCategoryService {
    @Autowired
    CategoryMapper categoryMapper;
    @Autowired
    StorageMapper storageMapper;

    @Override
    public CategoryListBean list() {
        int total = categoryMapper.categorycountAllByPid();
        int pages = 1;
        int limit = total;
        int page = 1;
        List<CategoryVO> list1 = categoryMapper.categoryqueryAll();
        Iterator<CategoryVO> iterator = list1.iterator();
        while (iterator.hasNext()) {
            CategoryVO categoryVO = iterator.next();
            Integer categoryVOid = categoryVO.getId();
            List<CategoryChildrenBean> list2 = categoryMapper.selectByChildrenBeanPid(categoryVOid);
            categoryVO.setChildren(list2);
        }
        return CategoryListBean.data(total, pages, limit, page, list1);

        //return null;
    }

    @Override
    public Category update(Category category) {
        Category newCategory = new Category(category.getId(), null, null, null, null, null, null,null,null,null,new Date(System.currentTimeMillis()), true);
        int affectedRows = categoryMapper.updateByPrimaryKeySelective(newCategory);
        if (affectedRows == 1) {
            return newCategory;
        }
        return null;
    }

    @Override
    public Integer deleteByPrimaryId(Integer id) {
        Category category = new Category(id,null,null,null,null,null,null,null,null,null,new Date(System.currentTimeMillis()),true
        );
        int i = categoryMapper.updateByPrimaryKeySelective(category);
        //int i = categoryMapper.deleteByPrimaryKey(id);
        return i;
    }


    @Override
    public List<CategoryL1> select() {
        List<CategoryL1> addressDate=categoryMapper.select();
        return addressDate;
    }

    @Override
    public int creat(Category category) {
        int i=categoryMapper.insertSelective(category);
        return i;
    }

    @Transactional(propagation = Propagation.REQUIRED)
    public Integer insertStorage(Storage storage) {
        storageMapper.insertSelective(storage);
        Integer id = storage.getId();
        return id;
    }
}
